######################
#Packages
######################
library(rio)
library(car)
library(ggpubr)
library(cowplot)
library(tidyverse)
library(ggstance)

###############
#Pew
###############

#Load Data
pew  <- import("./Data/pewcnn_probs_alt.dta") %>%
  rename(probs = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         pval = `_pvalue`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_at2`, 
         partisan = `_at1`, 
         outcome = `_predict`) %>%
  mutate(partisan = factor(partisan, 
                           levels=c(1,3), 
                           labels=c("Independent", "(Strong) Partisan")),
         ideol = factor(ideol, 
                        levels=c(1,2,3), 
                        labels=c("Moderate", "2", "Very Lib\nCons")), 
         outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", "One Party", "Both Parties"))) %>%
  select(probs, se, zstat, pval, lower, upper, ideol, partisan, outcome)


pew1 <- pew %>%
  ggplot(aes(x=ideol, y=probs, group=partisan, shape=partisan, linetype=partisan)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  facet_grid(.~outcome) + 
  labs(title = "Pew: Ideological Extremity", 
       y = NULL, x = NULL, shape = "Partisan", linetype="Partisan") +
  theme_pubclean(14) + 
  scale_y_continuous(limits=c(0,1)) + 
  scale_shape_manual(values=c(1,17))


###############
#Lucid
###############

#####Symbolic
luc_sym <- import("./Data/lucid_probs_ideolext.dta") %>%
  rename(probs = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         pval = `_pvalue`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_at2`, 
         partisan = `_at1`, 
         outcome = `_predict`) %>%
  mutate(ideol = factor(ideol, 
                        levels=c(1,2,3,4), 
                        labels=c("Moderate" , "2", "3", "Very Lib\n/Cons")), 
         partisan=factor(partisan, 
                         levels=c(1,4), 
                         labels=c("Inependent", "(Strong) Partisan")), 
         outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", "One Party", "Both Parties"))) %>%
  select(probs, se, zstat, pval, lower, upper, ideol, partisan, outcome)

lucid_symb_figure <- luc_sym %>%
  ggplot(aes(x=ideol, y=probs, group=partisan, shape=partisan, linetype=partisan)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  facet_grid(.~outcome) + 
  labs(title = "Lucid: Ideological Extremity", 
       y = NULL, x = NULL, shape = "Partisan", linetype="Partisan") +
  theme_pubclean(14) + 
  scale_shape_manual(values=c(1,17)) + 
  scale_y_continuous(limits=c(0,1)) 

#####Operational
lucid_op <- import("./Data/lucid_probs_op_ideol_cons.dta") %>%
  rename(probs = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         pval = `_pvalue`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_at2`, 
         partisan = `_at1`, 
         outcome = `_predict`) %>%
  mutate(ideol = factor(ideol, 
                        levels=c(0,1,2,3,4,5), 
                        labels=c("Mix.", "1", "2", "3", "4", "Cons.")), 
         partisan=factor(partisan, 
                         levels=c(1,4), 
                         labels=c("Inependent", "(Strong) Partisan")), 
         outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", "One Party", "Both Parties"))) %>%
  select(probs, se, zstat, pval, lower, upper, ideol, partisan, outcome)

lucid_op_fig <- lucid_op %>%
  ggplot(aes(x=ideol, y=probs, group=partisan, shape=partisan, linetype=partisan)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  facet_grid(.~outcome) + 
  labs(title = "Lucid: Operational Consistency", 
       y = NULL, x = NULL, shape = "Partisan", linetype="Partisan") +
  theme_pubclean(14) + 
  scale_shape_manual(values=c(1,17)) + 
  scale_y_continuous(limits=c(0,1))

#####PPD


####PPD
lucid_ppd <- import("./Data/lucid_probs_ppd.dta") %>%
  rename(probs = `_margin`, 
         se = `_se`, 
         zstat = `_statistic`, 
         pval = `_pvalue`, 
         lower = `_ci_lb`, 
         upper = `_ci_ub`, 
         ideol = `_at2`, 
         partisan = `_at1`, 
         outcome = `_predict`) %>%
  mutate(ideol = factor(ideol, 
                        levels=c(0,1,2,3,4,5,6), 
                        labels=c("Equal\nProx", "1", "2", "3", "4", "5", "Max R.\nProx" )), 
         partisan=factor(partisan, 
                         levels=c(1,4), 
                         labels=c("Inependent", "(Strong) Partisan")), 
         outcome = factor(outcome, 
                          levels=c(1,2,3), 
                          labels=c("Neither Party", "One Party", "Both Parties"))) %>%
  select(probs, se, zstat, pval, lower, upper, ideol, partisan, outcome)

lucid_ppd_fig <- lucid_ppd %>%
  ggplot(aes(x=ideol, y=probs, group=partisan, shape=partisan, linetype=partisan)) + 
  geom_pointrange(aes(ymin=lower, ymax=upper)) + 
  geom_line() + 
  facet_grid(.~outcome) + 
  labs(title = "Lucid: Perceived Policy Distance", 
       y = NULL, x = NULL, shape = "Partisan", linetype="Partisan") +
  theme_pubclean(14) + 
  scale_shape_manual(values=c(1,17)) + 
  scale_y_continuous(limits=c(0,1)) + 
  scale_x_discrete(breaks=c("Equal\nProx", "Max R.\nProx"))


###############
#Combining
###############
legendf1 <- get_legend(pew1 + theme(legend.text = element_text(size=16)))


ggarrange(pew1, lucid_symb_figure, 
          lucid_op_fig, 
          lucid_ppd_fig, 
          legend.grob = legendf1, 
          labels=c("A", "B", "C", "D"))


ggsave("Figure4.png", 
       height=8, width=12, dpi=1200)
